package com.landeqing.dao.impl;

import com.landeqing.dao.OrderAddressDao;
import com.landeqing.model.Address;
import com.landeqing.model.City;
import com.landeqing.model.Province;
import com.landeqing.model.Xian;
import com.landeqing.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class OrderAddressDaoImpl implements OrderAddressDao {
    /***
     * 添加收货地址
     * @param address
     * @return 1
     */
    public int save(Address address) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
         int result=   sqlSession.insert("com.landeqing.addressModel.save",address);
            sqlSession.commit();
            sqlSession.close();
         return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    /***
     * 查找收货地址
     * @return list
     */
    public List<Address> getList() {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
           List<Address> list=   sqlSession.selectList("com.landeqing.addressModel.selectList");
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public List<Province> getProvinceList() {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            List<Province> list=   sqlSession.selectList("com.landeqing.addressModel.selectProvinceList");
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public List<City> getCityListById(int p_id) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            List<City> list=   sqlSession.selectList("com.landeqing.addressModel.selectCityList",p_id);
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public List<Xian> getXianListById(int c_id) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            List<Xian> list=   sqlSession.selectList("com.landeqing.addressModel.selectXianList",c_id);
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public  static void main(String []args){
        OrderAddressDaoImpl dao=new OrderAddressDaoImpl();
        List<Xian> list=dao.getXianListById(2);
        for (Xian  xian:list
             ) {
            System.out.println(xian.getX_name());
        }
        List<Province> list2=dao.getProvinceList();
        for (Province  p:list2
                ) {
            System.out.println(p.getP_name());
        }
        List<City> cityList=dao.getCityListById(3);
        for (City c:cityList
             ) {
            System.out.println(c.getC_name());
        }
    }

    public Province getProvinceById(int id) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            Province   province=sqlSession.selectOne("com.landeqing.addressModel.selectProvinceOne",id);
            return  province;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public City getCityById(int id) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            City city=sqlSession.selectOne("com.landeqing.addressModel.selectCityOne",id);
            return city;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public Xian getXianById(int id) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            Xian xian=sqlSession.selectOne("com.landeqing.addressModel.selectXianOne",id);
            return  xian;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public boolean update(Address address) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
        int result=    sqlSession.update("com.landeqing.addressModel.update",address);
        if(result>0){
            sqlSession.commit();
            sqlSession.close();
            return true;

        }
        }catch (Exception E){
            E.printStackTrace();
        }
        return false;
    }

    public boolean delete(int id) {
        try{
                SqlSession sqlSession=MyBatisUtils.getSqlSession();
                int result=sqlSession.delete("com.landeqing.addressModel.delete",id);
                if (result>0){
                    sqlSession.commit();
                    sqlSession.close();
                return true;
                }
        }catch (Exception E){
            E.printStackTrace();
        }
        return false;
    }

    public Address getAddressById(int id) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            Address address=sqlSession.selectOne("com.landeqing.addressModel.getAddressById",id);
            return  address;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
